package com.blindBox.front.utils;

import org.springframework.stereotype.Component;

import java.util.Arrays;

//十六进制转换为10进制
@Component
public class VPC {

    public String setClassAble(String ids) {
        if (ids.equals("")) return "";
        int[] list = Arrays
                .stream(ids.split(" +"))
                .mapToInt(Integer::parseInt)
                .sorted()
                .toArray();
        int len = (list[list.length - 1] - 1) / 6 + 1;
        char[] chars = new char[len];
        for (int i = 1, id = 0; i <= len; ++i) {
            int val = 0;
            while (id < list.length && list[id] <= i * 6) {
                int how = list[id++] % 6;
                val |= (1 << (how == 0 ? 5 : how - 1));
            }
            chars[i-1] = parseX(val);
        }
        return new String(chars);
    }

    public String getAbleIDs(String str) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); ++i) {
            int anInt = parseInt(str.charAt(i));
            for (int j = 0; j < 6; ++j) {
                if ((anInt & (1 << j)) != 0)
                    sb.append(i * 6 + j + 1).append(' ');
            }
        }
        return sb.append('\b').toString();
    }

    public boolean ifIdAble(String str, int id) {
        //我也不知道这是啥 反正是对的
        return (str.length() * 6 >= id)&&((parseInt(str.charAt((id - 1) / 6)) & (1 << ((id % 6 == 0 ? 6 : id % 6) - 1))) > 0);
    }

    private static int parseInt(char c) {
        if (c >= '0' && c <= '9') return c - '0';
        else if (c >= 'a' && c <= 'z') return c - 'a' + 10;
        else if (c >= 'A' && c <= 'Z') return c - 'A' + 36;
        else if (c == '¥') return 62;
        else if (c == '$') return 63;
        return -1;
    }

    private static char parseX(int n) {
        if (n < 10) return (char) (n + '0');
        else if (n < 36) return (char) (n - 10 + 'a');
        else if (n < 62) return (char) (n - 36 + 'A');
        else if (n == 62) return '¥';
        else if (n == 63) return '$';
        return '?';
    }

}
